package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.BusinessException;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileService;
import com.xuecheng.media.service.MediaProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Mdd
 * @Github <a href="https://github.com/mdd123cc"/>
 * @Gitee <a href="https://gitee.com/mdd115192"/>
 */
@Service
public class MediaProcessServiceImpl extends ServiceImpl<MediaProcessMapper, MediaProcess> implements MediaProcessService {


    @Resource
    private MediaProcessMapper mediaProcessMapper;

    @Resource
    private MediaProcessService mediaProcessService;

    @Resource
    private MediaFileService mediaFileService;

    @Resource
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;

    @Override
    public List<MediaProcess> getFileProcessTask(int total, int index,int limit) {
        return mediaProcessMapper.selectListByShardIndex(total,index,limit);
    }


    @Override
    //实现如下
    public boolean startTask(long id) {
        int result = mediaProcessMapper.startTask(id);
        return result<=0?false:true;
    }

    @Transactional
    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        MediaProcess mediaProcess = mediaProcessService.getById(taskId);
        if (mediaProcess == null) {
            BusinessException.cast(CommonError.OBJECT_NULL);
        }
        //===============================处理失败===================================
        if (status.equals("3")){
            //修改status
            mediaProcess.setStatus(status);
            //失败次数+1
            mediaProcess.setFailCount(mediaProcess.getFailCount()+1);
            mediaProcess.setErrormsg(errorMsg);
            updateById(mediaProcess);
            return;
        }
        //===============================处理成功===================================
        //修改文件基本信息表中的url
        MediaFiles mediaFiles = new MediaFiles();
        mediaFiles.setUrl(url);
        mediaFiles.setId(fileId);
        mediaFileService.updateById(mediaFiles);
        //将记录添加到历史任务表
        mediaProcess.setUrl(url);
        mediaProcess.setStatus("2");
        mediaProcess.setFinishDate(LocalDateTime.now());
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
        int insert = mediaProcessHistoryMapper.insert(mediaProcessHistory);
        if (insert == 0) {
            BusinessException.cast(CommonError.UNKOWN_ERROR);
        }
        //删除media_process表中记录
        boolean res = removeById(taskId);
        if (!res) BusinessException.cast(CommonError.UNKOWN_ERROR);
    }
}
